tyNamespace.Player = cc.Class.extend({
    orderIndex: tyNamespace.OrderIndex.ZiJi,
    currentTile: null,                 //
    handTiles: null,                   //手中的牌
    tableTiles: null,                 //打出的牌自己前面
    getTiles: null,                    //碰杠的牌
    isAnGang: null,
    status: tyNamespace.PlayerStatus.AutoPlay,
    isZhuang: false,
    playLayer: null,
    userItem: null,
    lastMoTile: null,                  //最后一次摸的牌
    zmCount: 0,
    huCount: 0,
    dpCount: 0,
    sex: 0,
    huTile: null,
    isShowCards: false,

    ctor: function () {
        this.init();
    },

    init: function () {
        this.isShowCards = false;
        this.handTiles = [];
        this.tableTiles = [];
        this.getTiles = [];
        this.status = tyNamespace.PlayerStatus.Playing;
        this.currentTile = null;
    },

    setUserMoney: function (userItem) {
        this.money = userItem.lScore;
    },

    setUserName: function (userItem) {
        this.name = userItem.nickName;
    },

    setHandTiles: function (myTiles, initState) {
        if (initState) {
            for (var i = 0; i < myTiles.length; i++) {
                var tile = myTiles[i];
                tile.setBelong(this);
                tile.setStatus(tyNamespace.TileStatus.Li);
            }
        }

        this.handTiles = myTiles;
        tyNamespace.MjActions.sort(myTiles);
    },

    setHandCards: function (myTiles, initState) {
        if (initState) {
            for (var i = 0; i < myTiles.length; i++) {
                var tile = myTiles[i];
                tile.setBelong(this);

                if (this.isShowCards && this.orderIndex != 0) {
                    tile.setStatus(tyNamespace.TileStatus.Zhua);
                    continue;
                }

                tile.setStatus(tyNamespace.TileStatus.Li);
            }
        }

        this.handTiles = myTiles;
        gpNamespace.CardActions.sort(myTiles);
    },

    setDZHandCards: function (myTiles) {
        for (var i = 0; i < myTiles.length; i++) {
            var tile = myTiles[i];
        }

        this.handTiles = myTiles;
        gpNamespace.CardActions.sort(myTiles);
    },

    chu: function (tile) {
        this.moveTileToUnuse(tile);

//		cc.log("chu Player index:" + this.orderIndex + ", 出牌 ：" + tile.num + tile.type);
//
//		var tilesString = "[";
//		for (var i = 0; i < this.handTiles.length; i++) {
//			var tile = this.handTiles[i];
//			tilesString += tile.num + "" + tile.type + ",";
//		}
//		tilesString += "]";
//
//		cc.log(tilesString);
    },

    autoChu: function () {
        var danPai = tyNamespace.danPai;
        if (this.tableTiles.length < 0 || danPai == null || danPai.belong != this || danPai.status != tyNamespace.TileStatus.Li)
            danPai = tyNamespace.MjActions.chu(this.handTiles);

        this.chu(danPai);

        return danPai;
    },

    mo: function () {
        if (tyNamespace.unsetTiles.length == 0)
            return;

        var tile = tyNamespace.unsetTiles.shift();

        this.addTile(tile);

        this.playLayer.mo(tile);

        tyNamespace.remainedTileCount--;

        var remainedTileCount = this.playLayer.table.getChildByName("remainedTileCount");

        remainedTileCount.setString(tyNamespace.remainedTileCount);
    },

    moOnline: function (tile) {
        this.lastMoTile = tile;

        this.addTile(tile);

        var playLayer = this.playLayer;
        //playLayer.runAction(
        //		cc.sequence(
        //				cc.delayTime(0.2),
        //				cc.callFunc(function(playLayer){
        playLayer.addMoTile(tile);
        //				})
        //		)
        //);

        tyNamespace.remainedTileCount--;

        var remainedTileCount = this.playLayer.table.getChildByName("remainedTileCount");

        remainedTileCount.setString(tyNamespace.remainedTileCount);
    },

    getLastMoTile: function (tile) {
        if (this.lastMoTile == null)
            this.lastMoTile = this.handTiles[0];

        return this.lastMoTile;
    },

    addTile: function (tile) {
        this.handTiles.push(tile);
        tyNamespace.MjActions.sort(this.handTiles);
        tile.setBelong(this);
    },

    addTiles: function (tiles) {
        for (var i = 0; i < tiles.length; i++) {
            this.addTile(tiles[i]);
        }
    },

    moveTileToUnuse: function (tile) {
        tile.setStatus(tyNamespace.TileStatus.Da);
        this.tableTiles.push(tile);
        this.handTiles.splice(tile.index, 1);
        tyNamespace.MjActions.sort(this.handTiles);
    },

    removeTileWithSprite: function (tile) {
        tile.sprite.removeFromParent(true);
        this.handTiles.splice(tile.index, 1);

        tyNamespace.MjActions.sort(this.handTiles);
    },

    canOwnTile: function (tile) {
        if (this.canPeng(tile) != null)
            return true;

        return this.canHu(tile);
    },

    canHu: function (tile) {
        var clone = [];
        clone = clone.concat(this.handTiles);
        clone.push(tile);

        tyNamespace.MjActions.sort(clone);

        return !!tyNamespace.MjActions.isHu(clone);
    },

    canPeng: function (tile) {
        var bao = tyNamespace.MjActions.checkBao(tile, this.handTiles);
        if (bao.length == 3)
            return bao;

        return null;
    },

    canLeftChi: function (tile) {
        var shun = tyNamespace.MjActions.checkLeftShun(tile, this.handTiles);
        if (shun.length == 3)
            return shun;

        return null;
    },

    canMiddleChi: function (tile) {
        var shun = tyNamespace.MjActions.checkMiddleShun(tile, this.handTiles);
        if (shun.length == 3)
            return shun;

        return null;
    },

    canRightChi: function (tile) {
        var shun = tyNamespace.MjActions.checkRightShun(tile, this.handTiles);
        if (shun.length == 3)
            return shun;

        return null;
    },

    canGang: function (tile) {
        var gang = tyNamespace.MjActions.checkGang(tile, this.handTiles);

        if (gang.length == 4)
            return gang;

        return null;
    },

    canGang: function (tile, containTheTile) {
        var gang = tyNamespace.MjActions.checkGang(tile, this.handTiles);

        if (gang.length == 4)
            return gang;

        return null;
    },

    canBuGang: function (tile) {
        var index = -1;
        var tiles = this.getTiles;

        for (var i = 0; i < tiles.length; i++) {
            var currentT = tiles[i];

            if (currentT.typeValue == tile.typeValue && currentT.num == tile.num) {
                index = i;
                break;
            }
        }

        //var tilesString = "[";
        //for (var i = 0; i < tiles.length; i++) {
        //	var tile = tiles[i];
        //	tilesString += tile.num + "" + tile.type + ",";
        //}
        //tilesString += "]";
        //
        //cc.log(tilesString);

        if (index == 12)
            index = 9;
        else if (index > 0)
            index = 3 * parseInt(index / 3);

        return index;
    },

    onEventUserEnter: function (userItem) {
        cc.log("onUserEnter");
        this.userItem = userItem;
        this.orderIndex = userItem.wChairID;
        this.sex = userItem.cbGender;

        var runningScene = cc.director.getRunningScene();
        var preparegameLayer = runningScene.getChildByName(gpNamespace.GamePrepareLayerName);
        cc.log(preparegameLayer);
        preparegameLayer.setUser(this);//setUser:把人的信息展示出来
    },

    getScaleUserHeadNode: function (scale) {
        var size = cc.size(84 * scale, 82 * scale);
        this.getUserHeadNode(null, null, size);
    },

    getUserHeadNode: function (imgId, pannelSprite, size) {
        if (imgId == null)
            imgId = this.userItem.headId;

        if (size == null)
            size = cc.size(80, 80);

        var contectScale = 1;

        var userImg = new LoadHead(imgId);
        userImg.loadImg();
        var scaleSize = userImg.getContentSize();//cc.size(128, 128);
        userImg.setHeadWH(size.width / contectScale, size.height / contectScale);

//        if (pannelSprite == null)
//        	pannelSprite = gpNamespace.game.Game_userHeadBackground;

//        var userPanel = new cc.Sprite(pannelSprite);
//        userPanel.attr({
//            x: scaleSize.width / 2,
//            y: scaleSize.height / 2
//        });
//        var userPanelSize = userPanel.getContentSize();
//        userPanel.setScale(scaleSize.width * contectScale / userPanelSize.width, scaleSize.height * contectScale / userPanelSize.height);
//        userImg.addChild(userPanel);

        return userImg;
    },

    onUserUpdate: function (userItem) {
        this.userItem = userItem;
        if (this.userItem.wChairID != this.orderIndex) {
            this.userItem = null;
        }
    },

    onUserLeave: function (userItem) {
        cc.log("onUserLeave");

        this.userItem = null;
//		var runningScene = cc.director.getRunningScene();
//		var gameTitleLayer = runningScene.getChildByName("GameTitleLayer");
//
//		if(gameTitleLayer)
//			gameTitleLayer.removeUser();
    },

    isPlaying: function () {
        return this.userItem != null && this.userItem.cbUserStatus == EnumConstant.US_PLAY;
    },

    getRealTile: function (tile, index) {
        var myTiles = this.handTiles;
        if (index != undefined || index != null) {
            if (tile.typeValue == myTiles[index].typeValue && tile.num == myTiles[index].num) {
                return myTiles[index];
            }

            //myTiles[index].sprite.runAction(cc.moveBy(0.1, 0, -20));
        }

        for (var j = 0; j < myTiles.length; j++) {
            var currentTile = myTiles[j];
            if (currentTile.typeValue == tile.typeValue && currentTile.num == tile.num) {
                return currentTile;
            }
        }

        return null;
    },

    getRealTiles: function (tiles) {
        var resultTiles = [];
        var myTiles = this.handTiles;
        for (var i = 0; i < tiles.length; i++) {
            var tile = tiles[i];
            for (var j = 0; j < myTiles.length; j++) {
                var currentTile = myTiles[j];
                if (currentTile.typeValue == tile.typeValue && currentTile.num == tile.num && !tyNamespace.MjActions.haveTileByHash(currentTile, resultTiles)) {
                    resultTiles.push(currentTile);

                    break;
                }
            }
        }

        return resultTiles;
    },

    splice: function (from, deletecount) {
        var myTiles = this.handTiles;
        for (var i = from; i < myTiles.length && i < from + deletecount; i++) {
            var tile = myTiles[i];

            if (tile.sprite != null) {
                //cc.log("splice tile sprite index:" + i + ",sprite tag:" + tile.sprite.getTag() + ",player name:" + this.userItem.nickName);
                tile.sprite.removeFromParent();
            } else
                cc.log("splice tile sprite is null index:" + i);
        }

        myTiles.splice(from, deletecount);
    }
});